//
//  ThemeColorManager.swift
//  ServiceCode
//
//  Created by 曹连华 on 2020/12/21.
//

import UIKit
import CoreCode

@available(iOS 13.0, *)
@objc public class ThemeColorManager: NSObject {
    
    @objc public static let manager = ThemeColorManager()
    @objc public var currentThemeColor: Dictionary = ThemeColorManager.defourThemeColor()
    
    private override init() {}
    
    
    /* 更新当前选中的主题色 */
    @objc class func updateCurrentThemeColor(_ themeColor: Dictionary<String, String>?) {
        ThemeColorManager.manager.currentThemeColor = themeColor ?? self.defourThemeColor()
    }
    
    /* 默认主题色 */
    @objc class func defourThemeColor() -> Dictionary<String, String> {
        return ["main_color_1":"#2589FF",
                "main_color_1_dark":"#333333",
                "main_color_2":"#FE8885",
                "main_color_2_dark":"#FE8885",
                "main_color_3":"#FF5050",
                "main_color_3_dark":"#FF5050",
                "auxi_color_1":"#333333",
                "auxi_color_1_dark":"#333333",
                "auxi_color_2":"#61616A",
                "auxi_color_2_dark":"#61616A",
                "auxi_color_3":"#8A8A8F",
                "auxi_color_3_dark":"#8A8A8F",
                "auxi_color_4":"#CCCCCC",
                "auxi_color_4_dark":"#CCCCCC",
                "auxi_color_5":"#F7F7FA",
                "auxi_color_5_dark":"#F7F7FA"]
    }
    
    @objc class public func hexs(_ hexs:String) -> UIColor {
        
        let tup : (lightColor:UIColor,darkColor:UIColor) = ThemeColorManager.getLight_dark_color(hexs)
        return UIColor.init(dynamicProvider: { (traitCollection) -> UIColor in
            if traitCollection.userInterfaceStyle == .dark {
                return tup.lightColor
            } else {
                return tup.darkColor
            }
        })
    }
    
    /* 1.根据传入的 hexs(色值)获取 这个色值 在默认主题色表里对应的key(包括深色模式的key)
     * 2.拿到两个key后 从 当前选中的主题色表(currentThemeColor)里 获取与key 对应的 色值
     * 3.根据拿到的色值创建对应的颜色
     */
    class func  getLight_dark_color(_ hexs:String) -> (lightColor:UIColor,darkColor:UIColor) {
        //1.根据传入的 hexs(色值)获取 这个色值 在默认主题色表里对应的key(包括深色模式的key)
        var defour_light_colorKey : String = ""
        var defour_dark_colorKey: String   = ""
        for (key,value) in ThemeColorManager.defourThemeColor() {
            if value == hexs {
                defour_light_colorKey = key
                defour_dark_colorKey = "\(defour_light_colorKey)_dark"
            }
        }
        //2.拿到两个key后 从 当前选中的主题色表(currentThemeColor)里 获取与key 对应的 色值
        let current_light_colorValue = ThemeColorManager.manager.currentThemeColor[defour_light_colorKey] ?? hexs
        let current_dark_colorValue = ThemeColorManager.manager.currentThemeColor[defour_dark_colorKey] ?? hexs
        
        //3.根据拿到的色值创建对应的颜色
        return (UIColor.hex(current_light_colorValue),UIColor.hex(current_dark_colorValue))
    }
    
}

